/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_te_analyze_header.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"
#include "../test_tongeasy_comm.h"

#include <tongeasy/config.h>
#include <tongeasy/te_core.h>
#include <tongeasy/te_header.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    char *p_bin = (char *) "cf0700000a6fd2001421000064656d6f636c6900000000000064656d6f737672000000000000000033080000355a303231307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e312020202020202030303235202030202020302020203130202033302020746869732073796e7376722064617461207265715f66756e31";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(test_te_create_ct(&p_st_conn, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT), FR_SUCC);

    CU_ASSERT_EQUAL(te_header_detect(p_st_conn, arr_payload, strlen(p_bin) / 2), APM_L7_IS_YES);

    test_te_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * 标志位错误
 */
static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    char *p_bin = (char *) "ca0700000a6fd2001421000064656d6f636c6900000000000064656d6f737672000000000000000033080000355a303231307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e312020202020202030303235202030202020302020203130202033302020746869732073796e7376722064617461207265715f66756e31";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(test_te_create_ct(&p_st_conn, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT), FR_SUCC);

    CU_ASSERT_EQUAL(te_header_detect(p_st_conn, arr_payload, strlen(p_bin) / 2), APM_L7_IS_UNKNOWN);

    test_te_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * 心跳包监测-次数未到
 */
static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    char *p_bin = (char *) "000000000e002c0000000000000000000000000000000000000000000000000000000000000000003a000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    CU_ASSERT_EQUAL(test_te_create_ct(&p_st_conn, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT), FR_SUCC);

    CU_ASSERT_EQUAL(te_header_detect(p_st_conn, arr_payload, strlen(p_bin) / 2), APM_L7_IS_UNKNOWN);

    test_te_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_te_header_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" te_header_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
